// ----- INTERNAL INCLUDES ----- //
#include "functions.h"
#include "types.h"
#include "oskdialog.h"
#include "HTTP.h"
#include "packet.h"

// ----- DEFINES ----- //
#define N_IMAGES 4	  

// ----- STRUCTS ----- //
enum userAction{
		ACTION_MENU = 0,
		ACTION_TROPHY_LOAD,
		ACTION_TROHPY,
}uAction = ACTION_MENU;


// ----- VARIABLES ----- //
//Pad Data
padData pad;
padData oldpad;

//SDL Images
SDL_Surface *images[N_IMAGES]; // PNG container of texture3

//Store Status
int running = 1;
int menu_sel = 0;

//LOGIN Vars
OSK *cOSK = NULL;
char *username = NULL;
char *md5_pass = NULL;
char *temp_md5_pass = NULL;
s8 login_process = -1;

//Packet referent
CSocket *csocket = NULL;
int sock = 0;

// ----- FUNCTIONS ----- //
void customLoadPNG(){
	for(int i = 0; i < N_IMAGES; i++){
		char filename[1024] = {0};
		if(i==0) sprintf(filename, "/dev_hdd0/game/%s/USRDIR/BACKGROUND.PNG",hdd_folder_installed);
		if(i==1) sprintf(filename, "/dev_hdd0/game/%s/USRDIR/MENU.PNG",hdd_folder_installed);
		if(i==2) sprintf(filename, "/dev_hdd0/game/%s/USRDIR/MENUSEL.PNG",hdd_folder_installed);
		if(i==3) sprintf(filename, "/dev_hdd0/game/%s/USRDIR/LOGO.PNG",hdd_folder_installed);
		//if(i==4) sprintf(filename, "/dev_hdd0/game/%s/USRDIR/CRUCETA.PNG",hdd_folder_installed);
		//if(i==5) sprintf(filename, "/dev_hdd0/game/%s/USRDIR/X.PNG",hdd_folder_installed);
		//if(i==6) sprintf(filename, "/dev_hdd0/game/%s/USRDIR/O.PNG",hdd_folder_installed);
		//if(i==7) sprintf(filename, "/dev_hdd0/game/%s/USRDIR/SQ.PNG",hdd_folder_installed);
		images[i] = IMG_Load(filename);
	}
}

extern void login_result(unsigned char result){
	login_process = result;
	if(result == 2){
		//CCCC
		login_process = 0;
	}
}

void login_thread(void *arg){
	temp_md5_pass = send_login(username, md5_pass);

	sysThreadExit(0);
}

// ----- MAIN ----- //
int main(int argc, const char* argv[], const char* envp[]) {
	InitSDL();
	char filename[1024] = {0};
	sprintf(filename, "/dev_hdd0/game/%s/USRDIR",hdd_folder_installed);
	cOSK = new OSK(screen, filename);

	csocket = new CSocket();
	sock = csocket->remoteConnect("91.121.80.171", 80);
	
	Debug("SOCKETS INIT \n");
	osk_point point = {250, 100};
	cOSK->setPos(point);

login:

	for(int i = 0; i < 2; i++){
		pad = getPadData(0);
		cOSK->open();

		while(cOSK->getStatus() != OSK_RETURN){
			SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

			cOSK->draw(i);
			cOSK->handlePad();
			
			if(i == 0)
				fontPrintf(250, 70, "Input username...");
			else if(i == 1)
				fontPrintf(250, 70, "Input password...");

			SDL_Flip(screen);
			sysUtilCheckCallback();
		}

		if(i == 0){
			username = cOSK->getBuffer();
			usleep(500000);
		}else if(i == 1)
			md5_pass = cOSK->getBuffer();
	}

	login_process = -1;

	sys_ppu_thread_t login_thread_id;
	sysThreadCreate(&login_thread_id, login_thread, NULL, 1500, 0x3000, THREAD_JOINABLE, "LOGIN");

	while(login_process == -1){
		pad = getPadData(0);
	
		fontPrintf(250, 80, "Wait...");

		SDL_Flip(screen);
		sysUtilCheckCallback();
	}
	
	free(md5_pass);

	if (login_process == 0 || login_process == -1) {
		free(username);
		goto login;
	} else md5_pass = temp_md5_pass;
	

	/*** - MAIN LOOP - ***/
	while(running){
		pad = getPadData(0);
		SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

		apply_surface(0, 0, images[0], screen, NULL); //BACKGROUND

		int x = 625, y = 294;
		for(int i = 0; i < 4; i++){
			if(menu_sel == i) apply_surface(x, y, images[2], screen, NULL);
			else apply_surface(x, y, images[1], screen, NULL);
			if(i == 0) fontPrintf(x + 40, y + 41/2 - 28/2 - 5, "AMIGOS");
			else if(i == 1) fontPrintf(x + 40, y + 41/2 - 28/2 - 5, "TROFEOS");
			else if(i == 2) fontPrintf(x + 40, y + 41/2 - 28/2 - 5, "SAVES");
			else if(i == 3) fontPrintf(x + 40, y + 41/2 - 28/2 - 5, "NETWORK");
			y += 39;
		}
		x = 632;
		y = 215;
		fontPrintf(x, y, "Bienvenido: \n");
		fontPrintf(x + 30, y, username);
		switch(uAction){
			case ACTION_TROPHY_LOAD:{
				/**
				sys_ppu_thread_t id;
				u64 priority = 1500;
				size_t stack_size = 0x1000;
				sys_ppu_thread_create(  &id, trophie_sync, (u64)NULL, priority, stack_size, THREAD_JOINABLE, "TROPHY_SYNC");
				
				msgType mdialogprogress = MSGDIALOG_SINGLE_PROGRESSBAR;
				msgDialogOpen2(mdialogprogress, "Sincronizando trofeos", my_dialog, (void *) 0x33330001, NULL);
				msgDialogProgressBarMessage(PROGRESSBAR_INDEX0, "Espere");
				msgDialogResetProgressBar(PROGRESSBAR_INDEX0);

				dialog_action = 0;
				while(!dialog_action){
						sysUtilCheckCallback();
						SDL_Flip();
				}
				msgDialogClose();
			*/
			}break;
			default:
				break;
		}
		if(pad.BTN_DOWN && !oldpad.BTN_DOWN){
			if(menu_sel < 3) menu_sel++;
		}
		if(pad.BTN_UP && !oldpad.BTN_UP){
			if(menu_sel > 0) menu_sel--;
		}
		SDL_Flip(screen);
		sysUtilCheckCallback();
	}
	return 0;
}

